package com.nice.nice201110.controller;

import com.nice.nice201110.entity.TBaseInformation;
import com.nice.nice201110.service.TbaseInformationService;
import com.nice.nice201110.utils.NiceEnum;
import com.nice.nice201110.vo.BaseVo;
import com.nice.nice201110.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.*;

@Controller
@RestController
@RequestMapping("/admin/information")
public class TbaseInformationComtroller {

    @Autowired
    TbaseInformationService tbaseInformationService;

    private Map<String,Object> codeMes = new HashMap<>();

    /**
     *  查看信息的基本状况
     * */
    @ResponseBody
    @RequestMapping(method = RequestMethod.GET,value = "/list")
    public Map<String,Object> getlist(HttpServletRequest request, @RequestBody(required = false) BaseVo baseVo){
        HttpSession session = request.getSession();
        UserVo userVo = (UserVo)session.getAttribute("USER");
        if(userVo==null){
            codeMes.put("code", NiceEnum.code_300);
            codeMes.put("message",NiceEnum.NiceMap.get(NiceEnum.code_300));
            return codeMes;
        }
        if(baseVo!=null){
            baseVo.setUservo(userVo);
        }else{
            baseVo = new BaseVo();
            baseVo.setUservo(userVo);
        }
        List<TBaseInformation> tBaseInformation = tbaseInformationService.getlist(baseVo);

        codeMes.put("code", NiceEnum.code_200);
        codeMes.put("message", NiceEnum.NiceMap.get(NiceEnum.code_200));
        codeMes.put("data", tBaseInformation);
        return codeMes;

    }
    /**
     *   查询单一基本信息
     * */
    @ResponseBody
    @RequestMapping(method = RequestMethod.GET,value = "/one")
    public Map<String,Object> getOne(HttpServletRequest request,@RequestParam(name = "id",required=true) Integer id) {
        HttpSession session = request.getSession();
        UserVo userVo = (UserVo)session.getAttribute("USER");
        if(userVo==null){
            codeMes.put("code",NiceEnum.code_300);
            codeMes.put("message",NiceEnum.NiceMap.get(NiceEnum.code_300));
            return codeMes;
        }
        TBaseInformation tBaseInformation = tbaseInformationService.getOne(userVo,id);
        if(tBaseInformation!=null){
            codeMes.put("code", NiceEnum.code_200);
            codeMes.put("message", NiceEnum.NiceMap.get(NiceEnum.code_200));
            codeMes.put("data", tBaseInformation);
            return codeMes;
        }
        codeMes.put("code",NiceEnum.code_100);
        codeMes.put("message",NiceEnum.NiceMap.get(NiceEnum.code_100));
        return codeMes;
    }
    /** 基本信息 更新 */
    @ResponseBody
    @RequestMapping(method = RequestMethod.PUT,value = "/update")
    public Map<String,Object> updateTbaseInformation(HttpServletRequest request,@RequestBody TBaseInformation tBaseInformation){
        HttpSession session = request.getSession();
        UserVo userVo = (UserVo) session.getAttribute("USER");
        if(userVo==null){
            codeMes.put("code",NiceEnum.code_300);
            codeMes.put("message",NiceEnum.NiceMap.get(NiceEnum.code_300));
            return codeMes;
        }
        Boolean blean = tbaseInformationService.updateTbaseInformation(userVo,tBaseInformation);
        if(blean){
            codeMes.put("code", NiceEnum.code_200);
            codeMes.put("message", NiceEnum.NiceMap.get(NiceEnum.code_200));
            codeMes.put("data", tBaseInformation);
        }
        codeMes.put("code",NiceEnum.code_100);
        codeMes.put("message",NiceEnum.NiceMap.get(NiceEnum.code_100));
        return codeMes;
    }
    /** 基本信息插入 */
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST,value="/insert")
    public Map<String,Object> insertTbaseInformation(HttpServletRequest request,TBaseInformation tBaseInformation){
        HttpSession session = request.getSession();
        UserVo userVo = (UserVo) session.getAttribute("USER");
        if(userVo==null){
            codeMes.put("code",NiceEnum.code_300);
            codeMes.put("message",NiceEnum.NiceMap.get(NiceEnum.code_300));
            return codeMes;
        }
        Boolean blean = tbaseInformationService.insertTbaseInformation(userVo,tBaseInformation);
        if(blean){
            codeMes.put("code", NiceEnum.code_200);
            codeMes.put("message", NiceEnum.NiceMap.get(NiceEnum.code_200));
            codeMes.put("data", tBaseInformation);
        }
        codeMes.put("code",NiceEnum.code_100);
        codeMes.put("message",NiceEnum.NiceMap.get(NiceEnum.code_100));
        return codeMes;
    }
    /** 删除信息 */
    @ResponseBody
    @RequestMapping(method = RequestMethod.DELETE,value = "/delete")
    public Map<String,Object> deleteTbaseInformation(HttpServletRequest request,@RequestParam(name = "id",required=true) Integer id){
        HttpSession session = request.getSession();
        UserVo userVo = (UserVo) session.getAttribute("USER");
        if(userVo==null){
            codeMes.put("code",NiceEnum.code_300);
            codeMes.put("message",NiceEnum.NiceMap.get(NiceEnum.code_300));
            return codeMes;
        }
        Boolean blean = tbaseInformationService.deletetBaseInformation(userVo,id);
        if(blean){
            codeMes.put("code", NiceEnum.code_200);
            codeMes.put("message", NiceEnum.NiceMap.get(NiceEnum.code_200));
        }
        codeMes.put("code",NiceEnum.code_100);
        codeMes.put("message",NiceEnum.NiceMap.get(NiceEnum.code_100));
        return codeMes;
    }
    /** 删除信息s */
    @ResponseBody
    @RequestMapping(method = RequestMethod.DELETE,value = "/deletes")
    public Map<String,Object> deletesTbaseInformation(HttpServletRequest request,@RequestParam(name = "ids",required=true) String ids){
        HttpSession session = request.getSession();
        UserVo userVo = (UserVo) session.getAttribute("USER");
        if(userVo==null){
            codeMes.put("code",NiceEnum.code_300);
            codeMes.put("message",NiceEnum.NiceMap.get(NiceEnum.code_300));
            return codeMes;
        }
        Boolean blean = tbaseInformationService.deletetsBaseInformation(userVo,ids);
        if(blean){
            codeMes.put("code", NiceEnum.code_200);
            codeMes.put("message", NiceEnum.NiceMap.get(NiceEnum.code_200));
        }
        codeMes.put("code",NiceEnum.code_100);
        codeMes.put("message",NiceEnum.NiceMap.get(NiceEnum.code_100));
        return codeMes;
    }

}
